package com.ly.honor.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.honor.entity.AmbitusGoods;
import com.ly.honor.entity.dto.GoodsCreateDTO;
import com.ly.honor.entity.dto.GoodsEditDTO;
import com.ly.honor.entity.dto.GoodsQueryDTO;
import com.ly.honor.entity.vo.GoodsQueryVO;
import com.ly.honor.entity.vo.PlayerQueryVO;
import com.ly.honor.exception.HonorException;
import com.ly.honor.mapper.AmbitusGoodsMapper;
import com.ly.honor.service.AmbitusGoodsService;
import com.ly.honor.util.HonorUtil;
import com.ly.honor.util.ResultEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
* @author 17798
* @description 针对表【bs_ambitus_goods】的数据库操作Service实现
* @createDate 2023-03-31 17:13:43
*/
@Service
public class AmbitusGoodsServiceImpl extends ServiceImpl<AmbitusGoodsMapper, AmbitusGoods>
    implements AmbitusGoodsService {

    @Autowired
    private AmbitusGoodsMapper ambitusGoodsMapper;

    @Autowired
    private HonorUtil honorUtil;

    @Override
    public Page<GoodsQueryVO> getGoodsPage(long pageNum, long pageSize, GoodsQueryDTO goodsQueryDTO) {
        Page<GoodsQueryVO> page = new Page<>(pageNum,pageSize);
        Page<GoodsQueryVO> goodsQueryVOPage  = ambitusGoodsMapper.getGoodsPage(page,goodsQueryDTO);
        for (GoodsQueryVO goods:goodsQueryVOPage.getRecords()) {
            if(goods.getPicture() != null){
                goods.setPicture(goods.getPicture().split("\\?")[0]);
            }
        }
        return goodsQueryVOPage;
    }

    @Override
    public GoodsQueryVO getGoodsById(Integer id) {
        var ambitusGoods = ambitusGoodsMapper.selectById(id);
        if(ObjectUtils.isEmpty(ambitusGoods)){
            throw new HonorException("商品不存在");
        }
        var goodsQueryVO = new GoodsQueryVO();
        BeanUtils.copyProperties(ambitusGoods,goodsQueryVO);
        return goodsQueryVO;
    }

    @Override
    public void addGoods(GoodsCreateDTO goodsCreateDTO) {
        if(ObjectUtils.isEmpty(goodsCreateDTO)){
            throw new HonorException(ResultEnum.OBJECT_NOT_NULL);
        }
        var ambitusGoods = new AmbitusGoods();
        BeanUtils.copyProperties(goodsCreateDTO,ambitusGoods);
        if(ambitusGoods.getPicture() != null && ambitusGoods.getPicture().length() != 0){
            ambitusGoods.setPicture(ambitusGoods.getPicture().split("\\?")[0]);
        }
        ambitusGoodsMapper.insert(ambitusGoods);
    }

    @Override
    public void updateGoods(GoodsEditDTO goodsEditDTO) {
        if(ObjectUtils.isEmpty(goodsEditDTO)){
            throw new HonorException(ResultEnum.OBJECT_NOT_NULL);
        }
        var ambitusGoods = new AmbitusGoods();
        BeanUtils.copyProperties(goodsEditDTO,ambitusGoods);
        if(ambitusGoods.getPicture() != null && ambitusGoods.getPicture().length() != 0){
            ambitusGoods.setPicture(ambitusGoods.getPicture().split("\\?")[0]);
        }
        ambitusGoodsMapper.updateById(ambitusGoods);
    }

    @Override
    public String upload(MultipartFile file) {
        var url = honorUtil.uploadFile(file);
        return url;
    }
}




